package com.hancher.plugins.hrasm9log;

import org.jetbrains.annotations.NotNull;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.commons.AdviceAdapter;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.InnerClassNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.VarInsnNode;

public class HrAsmLogClassNode extends ClassNode {
    private final @NotNull ClassVisitor nextVisitor;

    public HrAsmLogClassNode(int api, @NotNull ClassVisitor classVisitor) {
        super(api);
        this.nextVisitor = classVisitor;
    }

    @Override
    public void visitEnd() {
        super.visitEnd();
        for (MethodNode methodNode : methods) {
           /* if ("onClick".equals(methodNode.name) && "(Landroid/view/View;)V".equals(methodNode.desc)) {
                System.out.println("========HrAsmLogClassVisitor==========visitMethod className = " + name + ", methodName=" + methodNode.name + ", methodDescriptor=" + methodNode.desc);
                methodNode.instructions.insertBefore(methodNode.instructions.getFirst(), new VarInsnNode(Opcodes.ALOAD,1));
                methodNode.instructions.insertBefore(methodNode.instructions.getFirst(), new MethodInsnNode(Opcodes.INVOKESTATIC, "com/hancher/plugins/hrasm9log/HrAsmLogUtil", "click", "(Landroid/view/View;)V", false));
            } else*/ if (methodNode.name.startsWith("lambda$") && "(Landroid/view/View;)V".equals(methodNode.desc)) {
                System.out.println("========HrAsmLogClassVisitor==========visitMethod className = " + name + ", methodName=" + methodNode.name + ", methodDescriptor=" + methodNode.desc);
                methodNode.instructions.insertBefore(methodNode.instructions.getFirst(), new VarInsnNode(Opcodes.ALOAD,0));
                methodNode.instructions.insertBefore(methodNode.instructions.getFirst(), new MethodInsnNode(Opcodes.INVOKESTATIC, "com/hancher/plugins/hrasm9log/HrAsmLogUtil", "click", "(Landroid/view/View;)V", false));
            }
        }
        accept(nextVisitor);
    }
}
